package com.sp.scmdb.serviceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.AlarmTypeBean;
import com.sp.scmdb.bean.ManulOutWellBean;
import com.sp.scmdb.bean.MonitorAreaBean;
import com.sp.scmdb.bean.MonitorBean;
import com.sp.scmdb.bean.MonitorDepBean;
import com.sp.scmdb.bean.MonitorEmpPositionBean;
import com.sp.scmdb.bean.MonitorReaderBean;
import com.sp.scmdb.bean.MonitorShiftsBean;
import com.sp.scmdb.bean.ReaderMapBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.AlarmDao;
import com.sp.scmdb.dao.AlarmTypeDao;
import com.sp.scmdb.dao.AreaDao;
import com.sp.scmdb.dao.CallAlarmDao;
import com.sp.scmdb.dao.EmpDayReportDao;
import com.sp.scmdb.dao.EntryRecordDao;
import com.sp.scmdb.dao.LabelReadingMachineDao;
import com.sp.scmdb.dao.ParameterDao;
import com.sp.scmdb.dao.ReaderAlarmDao;
import com.sp.scmdb.dao.RealDataDao;
import com.sp.scmdb.dao.ViewRealEmpDataDao;
import com.sp.scmdb.entity.Alarm;
import com.sp.scmdb.entity.AlarmType;
import com.sp.scmdb.entity.Area;
import com.sp.scmdb.entity.CallAlarm;
import com.sp.scmdb.entity.EmpDayReport;
import com.sp.scmdb.entity.EntryRecord;
import com.sp.scmdb.entity.LabelReadingMachine;
import com.sp.scmdb.entity.Parameter;
import com.sp.scmdb.entity.Reader_Alarm;
import com.sp.scmdb.entity.ViewRealEmpData;
import com.sp.scmdb.service.MonitorService;
import com.sp.scmdb.utils.ConvertMapToBean;

@Service("monitorService")
public class MonitorServiceImpl implements MonitorService {
	
	private LabelReadingMachineDao labelReadingMachineDao;
	
	/** 报警Dao */
	private AlarmDao alarmDao;

	/** 报警类型Dao */
	private AlarmTypeDao alarmTypeDao;

	/** 实时数据Dao */
	private RealDataDao realDataDao;

	/** 实时数据Dao */
	private ViewRealEmpDataDao viewRealEmpDataDao;

	/** 区域Dao */
	private AreaDao areaDao;

	/** 区域Dao */
	private LabelReadingMachineDao readerDao;

	/** 参数接口 **/
	private ParameterDao parameterDao;

	/** EntryRecordDao */
	private EntryRecordDao entryRecordDao;

	/** EmpDayReportDao */
	private EmpDayReportDao empDayReportDao;

	/** EmpDayReportDao */
	private ReaderAlarmDao raDao;

	private CallAlarmDao callAlarmDao;

	public List<AlarmType> getAlarmType() {
		List<AlarmType> listAlarmType = alarmTypeDao
				.queryHql("from AlarmType where isUse=1");
		return listAlarmType;
	}

	/**
	 * 根据报警类型的来获取该类型报警的数量
	 * 
	 * @param idAlarmType
	 *            报警类型ID
	 * @return 报警数量
	 */
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<AlarmTypeBean> getCountOfAlarm(List<AlarmType> listAlarmType) {

		AlarmTypeBean bean = null;
		List<AlarmTypeBean> listAlarmTypeBean = new ArrayList<AlarmTypeBean>();
		if (CollectionUtils.isNotEmpty(listAlarmType)) {
			for (int i = 0; i < listAlarmType.size(); i++) {
				bean = new AlarmTypeBean();
				bean.setId(listAlarmType.get(i).getId());
				bean.setAlarmName(listAlarmType.get(i).getAlarmName());
				bean.setEdEsc(listAlarmType.get(i).getEdEsc());
				bean.setIsUse(listAlarmType.get(i).getIsUse());
				if (bean.getId() == 9 || bean.getId() == 2) {
					bean.setAlarmCount(callAlarmDao.queryHql(
							"from CallAlarm where rmk='" + bean.getId()
									+ "' and confirmDate is null").size());
				} else {
					bean.setAlarmCount(alarmDao.queryHql(
							"from Alarm where 1=1 and   state = 0 and  alarmType="
									+ listAlarmType.get(i).getId()).size());
				}
				listAlarmTypeBean.add(bean);
			}
		}
		return listAlarmTypeBean;
	}

	/**
	 * 根据报警类型获取报警详细信息
	 */
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<Alarm> findAlarmByType(String idAlarmType) {
		List<Alarm> listAlarm = alarmDao.queryHql("from Alarm where alarmType="
				+ idAlarmType + " and  state = 0 ");

		return listAlarm;
	}

	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<Reader_Alarm> readerList() {
		List<Reader_Alarm> readerList = raDao
				.queryHql("from Reader_Alarm where endTime is null");
		return readerList;
	}

	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<CallAlarm> callAlarmList(String flag) {
		List<CallAlarm> callList = callAlarmDao
				.queryHql("from CallAlarm where rmk='" + flag
						+ "' and confirmDate is null");
		return callList;
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<ReaderMapBean> findReaderMapData() {
		List<ReaderMapBean> ret = new ArrayList<ReaderMapBean>();
		List<LabelReadingMachine> list = labelReadingMachineDao.queryAll();
		
		for(int i = 0; i <list.size(); ++i)
		{
			ReaderMapBean bean = new ReaderMapBean();
			bean.setReaderID(new Integer(list.get(i).getLrmID()).toString());
			bean.setMapID(new Integer(list.get(i).getMapID()).toString());
			ret.add(bean);
		}
		return ret;
		
	}
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public MonitorBean findRealData(MonitorBean bean, String empDep,
			String empWorkType, UserBean user) {
		String nowDate = new DateTime().plusMinutes(-5).toString(
				"yyyy-MM-dd HH:mm:ss");
		String Departments = user.getRole().getDepartment();
		if (StringUtils.isEmpty(Departments)) {
			// 如果当前用户的部门权限是空的，则不能看到任何信息
			bean.setCountAll("0");
			return bean;
		}
		// 获取当前园内总人数
		StringBuffer sql = new StringBuffer();
		sql.append("select count(empID) as countAllEmp \n"
				+ "   from VIEW_REAL_DATA \n" + "  where 1=1 and inTime>='"
				+ nowDate + "'\n");
		if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
			// 如果部门不为空，则按照选中的部门进行筛选
			sql.append("   and empDepID = '" + empDep + "' \n");
		}
		bean.setCountAll(String.valueOf(((Map) realDataDao.findBySql(
				sql.toString()).get(0)).get("countAllEmp").toString()));

		// 获取当前类型人数
		sql = new StringBuffer();
		sql.append("with TYPE_EMPCOUNT as ( \n"
				+ "   select count(empID) countEmp, empWorkTypeID as id \n"
				+ "     from VIEW_REAL_DATA \n" + "    where 1=1 and inTime>='"
				+ nowDate + "'\n");
		if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
			sql.append("   and empDepID = '" + empDep + "' \n");
		} 
		sql
				.append("   group by empWorkTypeID) \n"
						+ " select OBJ_WORKTYPE.id, OBJ_WORKTYPE.name, TYPE_EMPCOUNT.countEmp \n"
						+ "   from dbo.OBJ_WORKTYPE, TYPE_EMPCOUNT \n"
						+ " where  OBJ_WORKTYPE.id = TYPE_EMPCOUNT.id \n");
		List<MonitorShiftsBean> listShifts = ConvertMapToBean.convertMap(
				realDataDao.findBySql(sql.toString()), MonitorShiftsBean.class);
		bean.setShiftsEmp(listShifts);

		// 获取当前园内各读头(lrmType=1)信息
		List<LabelReadingMachine> listRead = readerDao
				.queryHql("from LabelReadingMachine");
		List<MonitorReaderBean> newList = new ArrayList();
		if (CollectionUtils.isNotEmpty(listRead)) {
			for (LabelReadingMachine reader : listRead) {
				sql = new StringBuffer();
				sql.append("select READER.id,  \n" + "	READER.lrmID, \n"
						+ "	READER.lrmName, \n" + "	READER.lrmType, \n"
						+ "	READER.lrmX,  \n" + "	READER.lrmY, \n"
						+ "	READER.lrmState, \n" + "	READER.lrmTime, \n" + "	READER.lrmRadius, \n");
				sql.append("(select count(empID) from VIEW_REAL_DATA \n"
						+ "  where VIEW_REAL_DATA.inTime>='" + nowDate
						+ "' and VIEW_REAL_DATA.lrmID = " + reader.getLrmID()
						+ "");

				if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
					sql.append("   and empDepID = '" + empDep + "' \n");
				}
				sql.append(") as empCountIn, ");

				sql.append("(select count(empID) from VIEW_REAL_DATA \n"
						+ "  where VIEW_REAL_DATA.inTime>='" + nowDate
						+ "' and VIEW_REAL_DATA.lrmID = " + reader.getLrmID()
						+ " and VIEW_REAL_DATA.empType = 1");

				if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
					sql.append("   and empDepID = '" + empDep + "' \n");
				}
				sql.append(") as empXMGL, ");
				sql.append("(select count(empID) from VIEW_REAL_DATA \n"
						+ "  where VIEW_REAL_DATA.inTime>='" + nowDate
						+ "' and VIEW_REAL_DATA.lrmID = " + reader.getLrmID()
						+ " and VIEW_REAL_DATA.empType = 2");

				if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
					sql.append("   and empDepID = '" + empDep + "' \n");
				}
				sql.append(") as empXMZY, ");
				sql.append("(select count(empID) from VIEW_REAL_DATA \n"
						+ "  where VIEW_REAL_DATA.inTime>='" + nowDate
						+ "' and VIEW_REAL_DATA.lrmID = " + reader.getLrmID()
						+ " and VIEW_REAL_DATA.empType = 3");

				if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
					sql.append("   and empDepID = '" + empDep + "' \n");
				}
				sql.append(") as empJL, ");
				sql.append("(select count(empID) from VIEW_REAL_DATA \n"
						+ "  where VIEW_REAL_DATA.inTime>='" + nowDate
						+ "' and VIEW_REAL_DATA.lrmID = " + reader.getLrmID()
						+ " and VIEW_REAL_DATA.empType = 4");

				if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
					sql.append("   and empDepID = '" + empDep + "' \n");
				}
				sql.append(") as empAQ, ");
				sql.append("(select count(empID) from VIEW_REAL_DATA \n"
						+ "  where VIEW_REAL_DATA.inTime>='" + nowDate
						+ "' and VIEW_REAL_DATA.lrmID = " + reader.getLrmID()
						+ " and VIEW_REAL_DATA.empType = 5");

				if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
					sql.append("   and empDepID = '" + empDep + "' \n");
				}
				sql.append(") as empWL, ");
				sql.append("(select count(empID) from VIEW_REAL_DATA \n"
						+ "  where VIEW_REAL_DATA.inTime>='" + nowDate
						+ "' and VIEW_REAL_DATA.lrmID = " + reader.getLrmID()
						+ " and VIEW_REAL_DATA.empType = 6");

				if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
					sql.append("   and empDepID = '" + empDep + "' \n");
				}
				sql.append(") as QC, ");
				sql.append(" 0 as empCountOut" + " from OBJ_READERS READER \n"
						+ "where lrmID=" + reader.getLrmID() + "");
				
				List<MonitorReaderBean> listReader = ConvertMapToBean
						.convertMap(realDataDao.findBySql(sql.toString()),
								MonitorReaderBean.class);
				for (MonitorReaderBean monitorReaderBean : listReader) {
					newList.add(monitorReaderBean);
				}
			}
		}
		bean.setListRead(newList);

		// 获取区域信息
		List<Area> listArea = areaDao.queryAll();
		List<MonitorAreaBean> listAreaBean = new ArrayList<MonitorAreaBean>();
		MonitorAreaBean areaBean = null;
		String empCount = "";
		for (int j = 0; j < listArea.size(); j++) {
			areaBean = new MonitorAreaBean();
			areaBean.setId(String.valueOf(listArea.get(j).getId()));
			areaBean.setName(listArea.get(j).getName());
			areaBean.setEmpCount(listArea.get(j).getEmpCount());
			areaBean.setTimeLength(listArea.get(j).getTimeLength());
			areaBean.setType(listArea.get(j).getType());
			areaBean.setReaders(listArea.get(j).getReaders());
			areaBean.setAreaX(listArea.get(j).getAreaX());
			areaBean.setAreaY(listArea.get(j).getAreaY());
			areaBean.setRelEmpCount(0);
			if (!areaBean.getReaders().isEmpty()) {
				sql = new StringBuffer();
				sql.append("SELECT count(empID) as countEmp \n"
						+ "   FROM VIEW_REAL_DATA VRD \n"
						+ " where 1 = 1 and inTime>='" + nowDate + "' \n");
				if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
					sql.append("   and empDepID = '" + empDep + "' \n");
				} 
				sql.append("  and VRD.lrmID IN (" + areaBean.getReaders()
						+ ") \n");
				empCount = ((Map) realDataDao.findBySql(sql.toString()).get(0))
						.get("countEmp").toString();
				if (!empCount.isEmpty()) {
					areaBean.setRelEmpCount(Integer.parseInt(empCount));
				}
			} else {
				areaBean.setRelEmpCount(0);
			}

			listAreaBean.add(areaBean);
		}
		bean.setListArea(listAreaBean);

		return bean;
	}

	/**
	 * 获取部门园内人数
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<MonitorDepBean> depCountList() {
		String nowDate = new DateTime().plusMinutes(-5).toString(
				"yyyy-MM-dd HH:mm:ss");
		StringBuffer sql = new StringBuffer();
		sql
				.append("select Dep.id,Dep.deptName,(select count(empID) from VIEW_REAL_DATA "
						+ "  where VIEW_REAL_DATA.inTime>='"
						+ nowDate
						+ "' and VIEW_REAL_DATA.empDepID = Dep.id) as empCount from OBJ_DEPARTMENT Dep");
		List<MonitorDepBean> listDep = ConvertMapToBean.convertMap(realDataDao
				.findBySql(sql.toString()), MonitorDepBean.class);

		return listDep;
	}

	/**
	 * 获取读卡器园内人数
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<MonitorReaderBean> readerCountList() {
		String nowDate = new DateTime().plusMinutes(-5).toString(
				"yyyy-MM-dd HH:mm:ss");
		StringBuffer sql = new StringBuffer();
		sql
				.append("select READER.id,READER.lrmID,READER.lrmName,READER.lrmType,READER.lrmX,"
						+ "READER.lrmY,READER.lrmState,READER.lrmTime,(select count(empID) from VIEW_REAL_DATA "
						+ "  where VIEW_REAL_DATA.inTime>='"
						+ nowDate
						+ "' and VIEW_REAL_DATA.lrmID = READER.lrmID");
		sql.append(") as empCountIn from OBJ_READERS READER ");
		List<MonitorReaderBean> listReader = ConvertMapToBean.convertMap(
				realDataDao.findBySql(sql.toString()), MonitorReaderBean.class);

		return listReader;
	}

	/**
	 * 获取园内所有人员信息
	 */
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<ViewRealEmpData> allEmpShow(UserBean user, String empDep,
			String empWorkType) {
		String nowDate = new DateTime().plusMinutes(-5).toString(
				"yyyy-MM-dd HH:mm:ss");
		// 园内所有人不包含园门人员，故需要判断读头类型为园内读头（lrmType=1）
		StringBuffer Hql = new StringBuffer(
				"from ViewRealEmpData where 1=1 and inTime>='" + nowDate
						+ "' \n");
		if (!"0".equals(empDep)) {
			Hql.append("and empDepID = " + empDep + " \n");
		}
		if (!StringUtils.isEmpty(empWorkType) && !"0".equals(empWorkType)) {
			Hql.append("and empWorkTypeID=" + empWorkType);
		}
		return viewRealEmpDataDao.queryHql(Hql.toString());
	}

	/**
	 * 获取指定班次的人员信息
	 */
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<ViewRealEmpData> shiftsEmpShow(String shiftsID, UserBean user,
			String empDep, String empWorkType) {
		String nowDate = new DateTime().plusMinutes(-5).toString(
				"yyyy-MM-dd HH:mm:ss");
		// 各班次人员不包含园门人员，故需要判断读头类型为园内读头（lrmType=1）
		StringBuffer Hql = new StringBuffer(
				"from ViewRealEmpData where inTime>='" + nowDate
						+ "' and empWorkTypeID='" + shiftsID + "'\n");

		if ("0".equals(empDep)) {
			Hql.append("and empDepID in (" + user.getRole().getDepartment()
					+ ") \n");
		} 
		if (!StringUtils.isEmpty(empWorkType) && !"0".equals(empWorkType)) {
			Hql.append("and empWorkTypeID=" + empWorkType);
		}
		return viewRealEmpDataDao.queryHql(Hql.toString());
	}

	/**
	 * 获取指定读卡器的人员信息
	 */
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<ViewRealEmpData> readerEmpShow(String readerID, String empDep,
			String empType, String empWorkType,UserBean user) {
		String nowDate = new DateTime().plusMinutes(-5).toString(
				"yyyy-MM-dd HH:mm:ss");
		StringBuffer Hql = new StringBuffer("from ViewRealEmpData where lrmID="
				+ readerID + " and inTime>='" + nowDate + "'"); // 离开时间为空

		if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
			Hql.append(" and empDepID = '" + empDep + "'");
		} 
		if (!StringUtils.isEmpty(empWorkType) && !"0".equals(empWorkType)) {
			Hql.append(" and empWorkTypeID = '" + empWorkType + "'");
		}
		if (!StringUtils.isEmpty(empType) && !"0".equals(empType)) {
			Hql.append(" and empType = '" + empType + "'");
		}

		return viewRealEmpDataDao.queryHql(Hql.toString());
	}

	/**
	 * 获取离开指定读卡器但是没有进入下一个读卡器范围的人员信息
	 */
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<ViewRealEmpData> readerOutEmpShow(String readerID,
			String empDep, String empWorkType, UserBean user) {
		StringBuffer Hql = new StringBuffer("from ViewRealEmpData where lrmID="
				+ readerID + " and outTime is not null"); // 离开时间不为空

		if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
			Hql.append(" and empDepID = '" + empDep + "'");
		} 
		if (!StringUtils.isEmpty(empWorkType) && !"0".equals(empWorkType)) {
			Hql.append(" and empWorkTypeID = '" + empWorkType + "'");
		}

		return viewRealEmpDataDao.queryHql(Hql.toString());
	}

	/**
	 * 获取指定区域的人员信息
	 */
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<ViewRealEmpData> areaEmpShow(String areaID, String empDep,
			String empWorkType, UserBean user) {
		String nowDate = new DateTime().plusMinutes(-5).toString(
				"yyyy-MM-dd HH:mm:ss");
		Area area = areaDao.getById(Integer.parseInt(areaID));
		StringBuffer Hql = new StringBuffer(
				"from ViewRealEmpData where inTime>='" + nowDate
						+ "' and lrmID in (" + area.getReaders() + ")");

		if (!StringUtils.isEmpty(empDep) && !"0".equals(empDep)) {
			Hql.append(" and empDepID = '" + empDep + "'");
		} 
		if (!StringUtils.isEmpty(empWorkType) && !"0".equals(empWorkType)) {
			Hql.append(" and empWorkTypeID = '" + empWorkType + "'");
		}

		return viewRealEmpDataDao.queryHql(Hql.toString());
	}

	/**
	 * 卡号、工号、姓名定位
	 * 
	 * @param text
	 *            查询内容
	 * @param type
	 *            1:卡号定位 2:姓名定位 3:工号定位
	 */
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<MonitorEmpPositionBean> searchByTag(String text, String type,
			UserBean user) {
		List<MonitorEmpPositionBean> resultList = new ArrayList<MonitorEmpPositionBean>();
		List<ViewRealEmpData> list = null;
		if ("1".equals(type)) {
			if (StringUtils.isNotEmpty(text)) {
				String Hql = "from ViewRealEmpData where (labelID like '%"
						+ text.trim() + "%' or empName like '%" + text.trim()
						+ "%') ";
				list = viewRealEmpDataDao.queryHql(Hql);
			}
		} else if ("2".equals(type)) {
			if (StringUtils.isNotEmpty(text)) {
				String Hql = "from ViewRealEmpData where empName like '%"
						+ text.trim() + "%' ";
				list = viewRealEmpDataDao.queryHql(Hql);
			}
		} else if ("3".equals(type)) {
			if (StringUtils.isNotBlank(text)) {
				String Hql = "from ViewRealEmpData where empWorkID like '%"
						+ text.trim() + "%' ";
				list = viewRealEmpDataDao.queryHql(Hql);
			}
		}
		if (list != null && list.size() > 0) {
			ViewRealEmpData data = null;
			MonitorEmpPositionBean bean = null;
			List<LabelReadingMachine> readers = null;
			for (int i = 0; i < list.size(); i++) {
				data = list.get(i);
				bean = new MonitorEmpPositionBean();
				bean.setId(data.getId().toString());
				bean.setEmpLabelID(data.getEmpLabelID());
				bean.setEmpName(data.getEmpName());
				bean.setEmpWorkID(data.getEmpWorkID());
				bean.setEmpDep(data.getEmpDep());
				bean.setEmpWorkType(data.getEmpWorkType());
				bean.setLrmID(data.getLrmID());

				readers = readerDao.queryByLrmID(data.getLrmID());
				if (readers.size() > 0) {
					bean.setXPos(String.valueOf(readers.get(0).getLrmX()));
					bean.setYPos(String.valueOf(readers.get(0).getLrmY()));
					bean.setPosition(readers.get(0).getLrmName());
				} else {
					bean.setXPos("10");
					bean.setYPos("10");
					bean.setPosition("");
				}

				resultList.add(bean);
			}
		}

		return resultList;
	}

	@Transactional
	public String confirmCallAlarm(String idAlarmType, String id) {
		String result = CommonParamter.CHECK_FAIL;
		if (StringUtils.isNotEmpty(id)) {
			if ("all".endsWith(id)) {
				callAlarmDao
						.excuteUpdate("update  OBJ_CALLALARM set confirmDate='"
								+ new DateTime()
										.toString("yyyy-MM-dd HH:mm:ss")
								+ "' where rmk='" + idAlarmType
								+ "' and confirmDate is null");
			} else {
				callAlarmDao
						.excuteUpdate("update  OBJ_CALLALARM set confirmDate='"
								+ new DateTime()
										.toString("yyyy-MM-dd HH:mm:ss")
								+ "' where rmk='" + idAlarmType
								+ "' and id in(" + id + ")");
			}
			result = CommonParamter.CHECK_SUCCESS;
		}
		return result;
	}

	@Transactional
	public String confirmAlarm(String idAlarmType, String id) {
		String result = CommonParamter.CHECK_FAIL;
		StringBuffer sb = new StringBuffer();
		sb.append("from Alarm  alarm where 1=1");
		if (StringUtils.isNotEmpty(id)) {
			// 全部确认
			if ("all".endsWith(id)) {
				sb.append(" and alarm.alarmType = " + idAlarmType);
				List<Alarm> list = alarmDao.queryHql(sb.toString());

				if (list.size() == 0) {
					return CommonParamter.CHECK_FAIL;
				}
				for (int i = 0; i < list.size(); i++) {
					Alarm alarm = list.get(i);
					alarm.setState(1);
					alarm.setOutTime(new DateTime());
					alarmDao.saveOrUpdate(alarm);
				}
				result = CommonParamter.CHECK_SUCCESS;
				// 防止走下个IF
				id = "";
			}
			// 单独确认
			if (id.length() > 0) {
				String[] ids = id.split(",");
				for (int i = 0; i < ids.length; i++) {
					String hql = "from Alarm alarm where 1=1 and alarm.id = "
							+ ids[i];
					List<Alarm> list = alarmDao.queryHql(hql);

					for (int j = 0; j < list.size(); j++) {
						Alarm alarm = list.get(j);
						alarm.setState(1);
						alarm.setOutTime(new DateTime());
						alarmDao.saveOrUpdate(alarm);
					}
					result = CommonParamter.CHECK_SUCCESS;
				}
			}
		}

		return result;
	}

	public Parameter getUsedPatameter() {
		Parameter para = new Parameter();
		String Hql = "from Parameter where asUse=1";
		List<Parameter> list = parameterDao.queryHql(Hql);
		if (CollectionUtils.isNotEmpty(list)) {
			para = parameterDao.queryHql(Hql).get(0);
		}
		return para;
	}

	/**
	 * 根据报警类型的ID取得报警类型
	 */
	public AlarmType getAlarmTypeById(String id) {
		return alarmTypeDao.getById(Integer.parseInt(id));
	}

	/**
	 * 获取园内所有人员信息
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<ViewRealEmpData> allEmpShow(UserBean user,
			ManulOutWellBean bean, ManulOutWellBean oldBean) {
		List<ViewRealEmpData> list = null;
		if (bean.equals(oldBean)) {
			list = oldBean.getAllList();
		} else {
			String name = bean.getEmpName();
			String empDep = bean.getEmpDepID();
			String labelID = bean.getEmpLabelID();

			// 园内所有人不包含园口人员，故需要判断读头类型为园内读头（lrmType=1）
			StringBuffer Hql = new StringBuffer(
					"from ViewRealEmpData where lrmType=1 \n");
			if (!StringUtils.isEmpty(name) && !"0".equals(name)) {
				Hql.append("and empName like'%" + name.trim() + "%' \n");
			}
			if (!StringUtils.isEmpty(empDep) || !"0".equals(empDep)) {
				Hql.append("and empDepID = " + empDep + " \n");
			}
			if (!StringUtils.isEmpty(labelID) && !"0".equals(labelID)) {
				Hql.append("and labelID like'%" + labelID.trim() + "%' \n");
			}
			Hql.append(" order by startTime ");
			list = viewRealEmpDataDao.queryHql(Hql.toString());
		}

		return list;
	}

	@Transactional
	public String manulOutWell(String empID) {
		try {
			StringBuffer hql = null;
			DateTime inTime = null;
			DateTime outTime = new DateTime();
			int labelID = 0;
			String[] strEmp = empID.split(",");
			// 修改EntryRecord
			for (String string : strEmp) {
				hql = new StringBuffer("from EntryRecord where empID=" + string
						+ " and outTime is null ");
				List<EntryRecord> list = entryRecordDao
						.queryHql(hql.toString());
				if (CollectionUtils.isNotEmpty(list)) {
					EntryRecord er = list.get(0);
					// 取的进园时间inTime可卡号
					inTime = er.getInTime();
					labelID = er.getLabelID();
					// 修该出园时间和备注
					er.setOutTime(outTime);
					er.setRmk("manulOutWell");
					// 保存EntryRecord
					entryRecordDao.update(er);
				}

				// 修改日报表
				Map<DateTime, DateTime> map = new HashMap<DateTime, DateTime>();
				EmpDayReport dayReport = new EmpDayReport();
				List<EmpDayReport> reportList = empDayReportDao
						.queryHql("from EmpDayReport where empID=" + string);
				if (CollectionUtils.isNotEmpty(reportList)) {
					dayReport = reportList.get(0);
					map = dayReport.getDataMap();
					boolean isUpdate = true;
					for (Map.Entry<DateTime, DateTime> ee : map.entrySet()) {
						if (inTime.isAfter(ee.getKey())
								&& inTime.isBefore(ee.getValue()))
							isUpdate = false;
					}
					if (isUpdate == true) {
						map.put(inTime, outTime);
						dayReport.setDataMap(map);
						empDayReportDao.update(dayReport);
					}
				} else {
					map.put(inTime, outTime);
					dayReport.setEmpID(Integer.parseInt(string));
					dayReport.setLabelID(labelID);
					dayReport.setDataMap(map);
					empDayReportDao.save(dayReport);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "10";
		}

		return "0";
	}

	public void setParameterDao(ParameterDao parameterDao) {
		this.parameterDao = parameterDao;
	}

	public void setAlarmTypeDao(AlarmTypeDao alarmTypeDao) {
		this.alarmTypeDao = alarmTypeDao;
	}

	public void setAlarmDao(AlarmDao alarmDao) {
		this.alarmDao = alarmDao;
	}

	public void setRealDataDao(RealDataDao realDataDao) {
		this.realDataDao = realDataDao;
	}

	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}

	public void setViewRealEmpDataDao(ViewRealEmpDataDao viewRealEmpDataDao) {
		this.viewRealEmpDataDao = viewRealEmpDataDao;
	}

	public void setReaderDao(LabelReadingMachineDao readerDao) {
		this.readerDao = readerDao;
	}

	public void setEntryRecordDao(EntryRecordDao entryRecordDao) {
		this.entryRecordDao = entryRecordDao;
	}

	public void setEmpDayReportDao(EmpDayReportDao empDayReportDao) {
		this.empDayReportDao = empDayReportDao;
	}

	public void setRaDao(ReaderAlarmDao raDao) {
		this.raDao = raDao;
	}

	@Autowired
	public void setCallAlarmDao(CallAlarmDao callAlarmDao) {
		this.callAlarmDao = callAlarmDao;
	}

	public void setLabelReadingMachineDao(LabelReadingMachineDao labelReadingMachineDao) {
		this.labelReadingMachineDao = labelReadingMachineDao;
	}

	public LabelReadingMachineDao getLabelReadingMachineDao() {
		return labelReadingMachineDao;
	}

}
